package com.service.user.impl;

import com.dao.GoodsReceiveInfoDao;
import com.entity.GoodsReceiveInfo;
import com.service.user.GRIService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/** 提供和收货信息相关的服务 */
@Service
public class GRIServiceImpl implements GRIService {

  /**
   * 根据收货信息编号获取收货信息
   *
   * @param grId 收货信息编号
   * @return 收货信息
   */
  @Override
  public GoodsReceiveInfo getAGoodsReceiveInfoById(String grId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    GoodsReceiveInfo gri = dao.selectAGoodsReceiveInfoById(grId);
    session.close();
    return gri;
  }

  /**
   * 获取指定用户的收货信息列表
   *
   * @param userId 用户编号
   * @return 用户的收货信息列表
   */
  @Override
  public List<GoodsReceiveInfo> getGoodsReceiveInfo(String userId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    List<GoodsReceiveInfo> grl = dao.selectGoodsReceiveInfo(userId);
    session.close();
    return grl;
  }

  /**
   * 修改收货信息
   *
   * @param userId 用户编号
   * @param grId 收货信息编号
   * @param grName 收货人姓名
   * @param grPhone 收货人电话号码
   * @param grProvinces 收货地址省份
   * @param grCity 收货地址地级市
   * @param grPrefecture 收货地址县级市
   * @param grSpecificAddr 收货地址具体位置
   * @param status 收货地址状态(1为该用户默认收获地址，0则不是)
   * @return 修改成功返回1，否则返回0
   */
  @Override
  public int chGoodsReceiveInfo(
      String userId,
      String grId,
      String grName,
      String grPhone,
      String grProvinces,
      String grCity,
      String grPrefecture,
      String grSpecificAddr,
      int status) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    if (status == 1) {
      dao.cancelDefault(userId);
    }
    int row =
        dao.updateGoodsReceiveInfo(
            grId, grName, grPhone, grProvinces, grCity, grPrefecture, grSpecificAddr, status);
    session.commit();
    session.close();
    return row;
  }

  /**
   * 将收货信息设置为默认
   *
   * @param userId 用户编号
   * @param grId 收货信息编号
   * @return 设置成功返回1，失败返回0
   */
  @Override
  public int setDefault(String userId, String grId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    int row;
    try {
      dao.cancelDefault(userId);
      row = dao.changeStatus(grId, 1);
      session.commit();
      session.close();
    } catch (Exception e) {
      return 500;
    }
    ;
    return row;
  }

  /**
   * 添加收货地址 参数列表同上
   *
   * @return 新添加收获地址的编号
   */
  @Override
  public String addGoodsReceiveInfo(
      String userId,
      String grName,
      String grPhone,
      String grProvinces,
      String grCity,
      String grPrefecture,
      String grSpecificAddr,
      int status) {
    // 生成随机的收货信息编号
    String grId = UUID.randomUUID().toString().replace("-", "");

    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    if (status == 1) {
      dao.cancelDefault(userId);
    }
    dao.insertGoodsReceiveInfo(
        userId, grId, grName, grPhone, grProvinces, grCity, grPrefecture, grSpecificAddr, status);
    session.commit();
    session.close();
    return grId;
  }

  /**
   * 删除一条收货信息
   *
   * @param grId 收货信息编号
   * @return 删除成功返回1，失败返回0
   */
  @Override
  public int delGoodsReceiveInfo(String grId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    GoodsReceiveInfoDao dao = session.getMapper(GoodsReceiveInfoDao.class);
    int row = dao.changeStatus(grId, -1);
    session.commit();
    session.close();
    return row;
  }
}
